package com.gurun.camera.account.presenter;

import android.os.Handler;

import com.danale.sdk.Danale;
import com.danale.sdk.platform.constant.user.UserType;
import com.danale.sdk.platform.constant.user.VerifyType;
import com.danale.sdk.platform.result.user.CheckVerificationCodeResult;
import com.danale.sdk.platform.result.user.ForgetPasswordResult;
import com.danale.sdk.platform.result.user.RegistGetVerificationCodeResult;
import com.danale.sdk.throwable.PlatformApiError;
import com.gurun.camera.account.model.ForgotModelImpl;
import com.gurun.camera.account.model.IForgotModel;
import com.gurun.camera.account.model.ISendVerifyCodeModel;
import com.gurun.camera.account.model.SendVerifycodeModelImpl;
import com.gurun.camera.account.view.IInputVerifycodeView;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

import static com.gurun.camera.account.presenter.VerifyAccountPresenterImpl.FORGOT_SEND_CODE;
import static com.gurun.camera.account.presenter.VerifyAccountPresenterImpl.REGIST_SEND_CODE;

/**
 *
 * Created by danal on 2016/9/9.
 */

public class InputVerifycodePresenterImpl implements IInputVerifycodePresenter {
    public static int RESEND_CODE = -1;
    private IInputVerifycodeView inputVerifycodeView;
    private ISendVerifyCodeModel sendVerifyCodeModel;
    private IForgotModel iForgotModel;
    private static final int UPDATE_TIME = 1;
    private int time = 60;

    public InputVerifycodePresenterImpl(IInputVerifycodeView view) {
        this.inputVerifycodeView = view;
        sendVerifyCodeModel = new SendVerifycodeModelImpl();
        iForgotModel = new ForgotModelImpl();
    }


    /**
     * 倒计时60 秒后重发
     * Countdown to 60 seconds resend
     */

    @Override
    public void countDownResend() {

        mHandler.sendEmptyMessageDelayed(UPDATE_TIME, 101);

    }

    public void countDownStop() {
        time = 0;
    }

    /**
     * 校验验证码
     * check Verification code
     * @param userName         用户名
     *                         account phone or email
     * @param phoneCode        国别码
     *                         If userName is the phone number, phonecode is the country code of the country where the phone number belongs.
     *                         If userName is a email, phonecode is empty;
     * @param verifyType       验证码类型
     *                         register or forgot password
     * @param verificationCode 验证码
     *                         user input verification code
     */
    @Override
    public void checkVerifyCode(String userName, String phoneCode, final VerifyType verifyType, String verificationCode) {
        inputVerifycodeView.hideIllegalLayout();
        Danale.get().getAccountService().checkVerificationCode(0, userName, phoneCode, verifyType, verificationCode)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<CheckVerificationCodeResult>() {
                    @Override
                    public void call(CheckVerificationCodeResult checkVerificationCodeResult) {
                        if (inputVerifycodeView != null) {

                            inputVerifycodeView.notifyCheckVerifyCodeResult("SUCCESS");
                        }


                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (throwable instanceof PlatformApiError) {
                            if (inputVerifycodeView != null) {

                                inputVerifycodeView.notifyCheckVerifyCodeResult(((PlatformApiError) throwable).getErrorDescription());
                            }
                        }

                    }
                });
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            if (time >= 0) {
                if (msg.what == UPDATE_TIME) {
                    inputVerifycodeView.notifyResendVerifyCodeState(time);

                    time -= 1;
                    if (time == 0) {
                        reObainCode();
                    } else {
                        sendEmptyMessageDelayed(UPDATE_TIME, 1000l);

                    }
                }
            } else {
                reObainCode();
            }
            super.handleMessage(msg);
        }

        private void reObainCode() {
            time = 60;
            inputVerifycodeView.notifyResendVerifyCodeState(RESEND_CODE);
        }

    };

    public void sendVerifycode(String account, UserType type, String countrycode, int sendtype) {
        inputVerifycodeView.hideIllegalLayout();

        if (sendtype == REGIST_SEND_CODE) {

            sendVerifyCodeModel.sendVerifyCode(account, type, countrycode)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<RegistGetVerificationCodeResult>() {
                        @Override
                        public void call(RegistGetVerificationCodeResult registGetVerificationCodeResult) {

                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {

                            if (throwable instanceof PlatformApiError) {


                            }
                        }
                    });

        } else if (sendtype == FORGOT_SEND_CODE) {

            iForgotModel.sendForgotPwdCode(account, type, countrycode)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<ForgetPasswordResult>() {
                        @Override
                        public void call(ForgetPasswordResult forgetPasswordResult) {


                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {

                        }
                    });

        }

    }

}
